﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using VacationDaysCommons;
using VacationDaysDatabase;
using VacationDaysCommons.VDExceptions;
using VacationDaysCommons.Faults;

namespace VacationDays
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class VDUserService : IVDUserService
    {
        public VDEmployee LoginUser(string username, string password)
        {
            var userInfo = DataAccess.Instance.GetEmployee(username, password);
            return userInfo;
        }

        public void LogoutUserSession(Guid sessionId)
        {
            //do something with the session ID, and log the user out
            // then send event to inform
            return;
        }

        public VDEmployee CreateNewUser(VDEmployee newUserData)
        {
            try
            {
                VDEmployee userInfo = DataAccess.Instance.TryCreateNewUser(newUserData);
                return userInfo;
            }
            catch (VDUsernameExistsException)
            {
                throw new FaultException<VDUsernameAlreadyExistsFault>(new VDUsernameAlreadyExistsFault());
            }
            catch (VDEmailExistsException)
            {
                throw new FaultException<VDEmailAlreadyExistsFault>(new VDEmailAlreadyExistsFault());
            }
            catch (VDUnknownErrorException)
            {
                throw new FaultException<VDUnknownErrorFault>(new VDUnknownErrorFault());
            }
            catch (Exception e)
            {
                throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message);
            }
        }

        public bool UpdateUser(VDEmployee updatedUserInfo)
        {
            return DataAccess.Instance.TryUpdateUser(updatedUserInfo);
        }

        public List<VDEmployee> GetEmployees(int startIndex, int count)
        {
            return DataAccess.Instance.GetEmployees(startIndex, count);
        }

        public List<VDEmployee> GetProjectManagers()
        {
            return DataAccess.Instance.GetProjectManagers();
        }

        public List<VDEmployee> GetEmployeesByPM(Guid PMId)
        {
            return DataAccess.Instance.GetEmployeesByPM(PMId);
        }
    }
}
